---
title: "Subgroup analysis: risk talk"
author: "[redacted]"
date: "2025-04-07"
output: html_document
---

```{r}
```

#####LOAD PACKAGES AND DATA

```{r}

#Load packages
library(tidyverse)
library(cjoint)
library(cregg)

conjoint_know <- cjoint::read.qualtrics(
  "data_clean.csv",
  covariates = c(
                  "risk_knowl_1_fipl_1", 
                  "risk_knowl_1_fipl_2", 
                  "risk_knowl_1_fipl_3", 
                  "risk_knowl_1_fipl_4", 
                  "risk_knowl_2_filp_1", 
                  "risk_knowl_2_filp_2", 
                  "risk_knowl_2_filp_3", 
                  "risk_knowl_2_filp_4",
                  "risk_knowl_3_fpil_1", 
                  "risk_knowl_3_fpil_2", 
                  "risk_knowl_3_fpil_3", 
                  "risk_knowl_3_fpil_4",
                  "risk_knowl_4_fpli_1", 
                  "risk_knowl_4_fpli_2", 
                  "risk_knowl_4_fpli_3", 
                  "risk_knowl_4_fpli_4",
                  "risk_knowl_5_flip_1",         
                  "risk_knowl_5_flip_2", 
                  "risk_knowl_5_flip_3", 
                  "risk_knowl_5_flip_4",
                  "risk_knowl_6_flpi_1",
                  "risk_knowl_6_flpi_2", 
                  "risk_knowl_6_flpi_3", 
                  "risk_knowl_6_flpi_4",
                  "risk_knowl_7_ifpl_1",
                  "risk_knowl_7_ifpl_2", 
                  "risk_knowl_7_ifpl_3", 
                  "risk_knowl_7_ifpl_4",
                  "risk_knowl_8_iflp_1",
                  "risk_knowl_8_iflp_2", 
                  "risk_knowl_8_iflp_3", 
                  "risk_knowl_8_iflp_4",
                  "risk_knowl_9_ipfl_1",
                  "risk_knowl_9_ipfl_2", 
                  "risk_knowl_9_ipfl_3", 
                  "risk_knowl_9_ipfl_4",
                  "risk_knowl_10_iplf_1",
                  "risk_knowl_10_iplf_2", 
                  "risk_knowl_10_iplf_3", 
                  "risk_knowl_10_iplf_4",
                  "risk_knowl_11_ilfp_1",
                  "risk_knowl_11_ilfp_2", 
                  "risk_knowl_11_ilfp_3", 
                  "risk_knowl_11_ilfp_4",
                  "risk_knowl_12_ilpf_1",
                  "risk_knowl_12_ilpf_2", 
                  "risk_knowl_12_ilpf_3", 
                  "risk_knowl_12_ilpf_4",
                  "risk_knowl_13_pfil_1",
                  "risk_knowl_13_pfil_2", 
                  "risk_knowl_13_pfil_3", 
                  "risk_knowl_13_pfil_4",
                  "risk_knowl_14_pfli_1",
                  "risk_knowl_14_pfli_2", 
                  "risk_knowl_14_pfli_3", 
                  "risk_knowl_14_pfli_4",
                  "risk_knowl_15_pifl_1",
                  "risk_knowl_15_pifl_2", 
                  "risk_knowl_15_pifl_3", 
                  "risk_knowl_15_pifl_4",
                  "risk_knowl_16_pilf_1",
                  "risk_knowl_16_pilf_2", 
                  "risk_knowl_16_pilf_3", 
                  "risk_knowl_16_pilf_4",
                  "risk_knowl_17_plfi_1",
                  "risk_knowl_17_plfi_2", 
                  "risk_knowl_17_plfi_3", 
                  "risk_knowl_17_plfi_4",
                  "risk_knowl_18_plif_1",
                  "risk_knowl_18_plif_2", 
                  "risk_knowl_18_plif_3", 
                  "risk_knowl_18_plif_4",
                  "risk_knowl_19_lfip_1",
                  "risk_knowl_19_lfip_2", 
                  "risk_knowl_19_lfip_3", 
                  "risk_knowl_19_lfip_4",
                  "risk_knowl_20_lfpi_1",
                  "risk_knowl_20_lfpi_2", 
                  "risk_knowl_20_lfpi_3", 
                  "risk_knowl_20_lfpi_4",
                  "risk_knowl_21_lifp_1",
                  "risk_knowl_21_lifp_2", 
                  "risk_knowl_21_lifp_3", 
                  "risk_knowl_21_lifp_4",
                  "risk_knowl_22_lipf_1",
                  "risk_knowl_22_lipf_2", 
                  "risk_knowl_22_lipf_3", 
                  "risk_knowl_22_lipf_4",
                  "risk_knowl_23_lpfi_1",
                  "risk_knowl_23_lpfi_2", 
                  "risk_knowl_23_lpfi_3", 
                  "risk_knowl_23_lpfi_4",
                  "risk_knowl_24_lpif_1",
                  "risk_knowl_24_lpif_2", 
                  "risk_knowl_24_lpif_3", 
                  "risk_knowl_24_lpif_4"),  
  respondentID = "ResponseId",
  letter = "F",
  new.format = TRUE,
  responses = paste0("conjoint_", c("intro", 1:8))
)

#For some reason, R keeps changing the name of the variable "risk_knowl_1_fipl_2" to "respondent". I present the ingenious solution here:
colnames(conjoint_know)[colnames(conjoint_know) == "respondent"] <- "risk_knowl_1_fipl_2"

names(conjoint_know)


```

#RECODE COVARIATE

```{r}

###RISK KNOWLEDGE
# Create empty variables for the original questions in the conjoint dataset
conjoint_know$know_flooding <- NA
conjoint_know$know_infectious <- NA
conjoint_know$know_pesticides <- NA
conjoint_know$know_lifestyle <- NA

# Map each block to its order
question_order <- c(
  'fipl', 'filp', 'fpil', 'fpli', 'flip', 'flpi',
  'ifpl', 'iflp', 'ipfl', 'iplf', 'ilfp', 'ilpf',
  'pfil', 'pfli', 'pifl', 'pilf', 'plfi', 'plif',
  'lfip', 'lfpi', 'lifp', 'lipf', 'lpfi', 'lpif'
)

# Convert all risk knowledge columns to numeric
for (block in 1:24) {
  for (position in 1:4) {
    risk_know_var <- paste0("risk_knowl_", block, "_", question_order[block], "_", position)
    
    # Check if the column exists
    if (risk_know_var %in% colnames(conjoint_know)) {
      # Convert to numeric, skipping columns with 0 rows
      if (!is.null(conjoint_know[[risk_know_var]]) && length(conjoint_know[[risk_know_var]]) > 0) {
        conjoint_know[[risk_know_var]] <- as.numeric(as.character(conjoint_know[[risk_know_var]]))
      } else {
        print(paste("Warning: Column", risk_know_var, "does not have the expected number of rows"))
      }
    } else {
      print(paste("Warning: Column", risk_know_var, "does not exist in the dataset"))
    }
  }
}

### Populate the new `rknow_*` variables for flooding, infectious diseases, pesticides, and lifestyle
for (block in 1:24) {
  order <- strsplit(question_order[block], "")[[1]]  # Break the block into its components (f, i, p, l)
  
  for (position in 1:4) {
    risk_know_var <- paste0("risk_knowl_", block, "_", question_order[block], "_", position)
    
    # Find indices where the value is not NA
    indices <- !is.na(conjoint_know[[risk_know_var]])
    
    # Assign values to the correct knowrob_* and rs_* variables based on the type (f = flooding, i = infectious, etc.)
    if (order[position] == "f") {
      conjoint_know$rknow_flooding[indices] <- conjoint_know[[risk_know_var]][indices]
    } else if (order[position] == "i") {
      conjoint_know$rknow_infectious[indices] <- conjoint_know[[risk_know_var]][indices]
    } else if (order[position] == "p") {
      conjoint_know$rknow_pesticides[indices] <- conjoint_know[[risk_know_var]][indices]
    } else if (order[position] == "l") {
      conjoint_know$rknow_lifestyle[indices] <- conjoint_know[[risk_know_var]][indices]
    }
  }
}


summary(conjoint_know$rknow_flooding)
summary(conjoint_know$rknow_infectious)
summary(conjoint_know$rknow_pesticides)
summary(conjoint_know$rknow_lifestyle)


conjoint_know <- conjoint_know %>% drop_na(rknow_flooding)
conjoint_know <- conjoint_know %>% drop_na(rknow_infectious)
conjoint_know <- conjoint_know %>% drop_na(rknow_pesticides)
conjoint_know <- conjoint_know %>% drop_na(rknow_lifestyle)


conjoint_know <- conjoint_know %>%
  mutate(
    know_cat_flood = ifelse(rknow_flooding >= 0 & rknow_flooding <= 2, "Low flood knowledge",
              ifelse(rknow_flooding >= 3 & rknow_flooding <= 4, "High flood knowledge", NA)),
    know_cat_flood = factor(know_cat_flood, levels=c("Low flood knowledge", "High flood knowledge"))  # Convert to a factor
  )

conjoint_know <- conjoint_know %>%
  mutate(
    know_cat_infect = ifelse(rknow_infectious >= 0 & rknow_infectious <= 2, "Low infectious disease knowledge",
              ifelse(rknow_infectious >= 3 & rknow_infectious <= 4, "High infectious disease knowledge", NA)),
    know_cat_infect = factor(know_cat_infect, levels=c("Low infectious disease knowledge", "High infectious disease knowledge"))  # Convert to a factor
  )

conjoint_know <- conjoint_know %>%
  mutate(
    know_cat_pest = ifelse(rknow_pesticides >= 0 & rknow_pesticides <= 2, "Low pesticide knowledge",
              ifelse(rknow_pesticides >= 3 & rknow_pesticides <= 4, "High pesticide knowledge", NA)),
    know_cat_pest = factor(know_cat_pest, levels=c("Low pesticide knowledge", "High pesticide knowledge"))  # Convert to a factor
  )

conjoint_know <- conjoint_know %>%
  mutate(
    know_cat_life = ifelse(rknow_lifestyle >= 0 & rknow_lifestyle <= 2, "Low lifestyle disease knowledge",
              ifelse(rknow_lifestyle >= 3 & rknow_lifestyle <= 4, "High lifestyle disease knowledge", NA)),
    know_cat_life = factor(know_cat_life, levels=c("Low lifestyle disease knowledge", "High lifestyle disease knowledge"))  # Convert to a factor
  )

summary(conjoint_know$know_cat_flood)
summary(conjoint_know$know_cat_infect)
summary(conjoint_know$know_cat_pest)
summary(conjoint_know$know_cat_life)


###GENERAL KNOWLEDGE VARIABLE

# Calculate the average of the four composite variables
conjoint_know$knowledge <- rowMeans(conjoint_know[, c("rknow_flooding", 
                                                   "rknow_infectious", 
                                                   "rknow_pesticides", 
                                                   "rknow_lifestyle")], na.rm = TRUE)

mean(conjoint_know$knowledge, na.rm = TRUE)

conjoint_know <- conjoint_know %>%
  mutate(
    know_cat = ifelse(knowledge >= 0 & knowledge < 3, "Less well-informed",
              ifelse(knowledge >= 3 & knowledge <= 4, "More well-informed", NA)),
    know_cat = factor(know_cat, levels = c("Less well-informed", "More well-informed"))  # Convert know_cat to a factor
  )

summary(conjoint_know$know_cat)


```

#RENAME VARIABLES

```{r}

# Renaming attributes and levels in English
conjoint_know <-  conjoint_know %>% dplyr::rename(Topic=Onderwerp, 
                                            Relation.to.you=Relatie.tot.u,
                                            Apparent.knowledge.of.the.person=Schijnbare.kennis.van.de.persoon,
                                            Apparent.concern.of.the.person=Schijnbare.bezorgdheid.van.de.persoon,
                                            Apparent.motivation.of.the.person=Schijnbare.motivatie.van.de.persoon,
                                            Closeness.with.you=Band.met.u,
                                            Gender.of.the.person=Geslacht.van.de.persoon
                                            ) %>%
                                     mutate(
                                            Topic = recode_factor(Topic,
                                                  "overstromingen" = "floods", 
                                                  "besmettelijke ziekten" = "infectious diseases",
                                                  "pesticiden" = "pesticides",
                                                  "levensstijlziekten" = "lifestyle diseases",),
                                            Gender.of.the.person = recode_factor(Gender.of.the.person,
                                                  "vrouw" = "female", 
                                                  "man" = "male"),
                                            Relation.to.you = recode_factor(Relation.to.you,
                                                  "vriend/vriendin (platonisch)" = "friend",
                                                  "collega/studiegenoot" = "colleague/fellow student",
                                                  "familielid" = "relative"),
                                            Closeness.with.you = recode_factor(Closeness.with.you,
                                                  "zeer hecht" = "very close", 
                                                  "redelijk hecht" = "fairly close",
                                                  "enigszins hecht" = "somewhat close",
                                                  "niet hecht" = "not close"),
                                            Apparent.knowledge.of.the.person = recode_factor(Apparent.knowledge.of.the.person,
                                                  "zeer goed geinformeerd" = "very well-informed", 
                                                  "redelijk goed geinformeerd" = "fairly well-informed",
                                                  "enigszins geinformeerd" = "somewhat well-informed",
                                                  "niet geinformeerd" = "not well-informed"),
                                            Apparent.concern.of.the.person = recode_factor(Apparent.concern.of.the.person,
                                                  "zeer bezorgd" = "very concerned", 
                                                  "redelijk bezorgd" = "fairly concerned",
                                                  "enigszins bezorgd" = "somewhat concerned",
                                                  "niet bezorgd" = "not concerned"),
                                            Apparent.motivation.of.the.person = recode_factor(Apparent.motivation.of.the.person,
                                                  "u te overtuigen om meer maatregelen te nemen" = "to convince you to take more precautions", 
                                                  "informatie over het risico met u te willen uitwisselen" = "to exchange information with you",
                                                  "zich minder ongerust voelen" = "to feel less anxious",
                                                  "u te overtuigen om minder maatregelen te nemen" = "to convince you to take fewer precautions"))


#Check renamings
table(conjoint_know$Topic)
table(conjoint_know$Gender.of.the.person)
table(conjoint_know$Relation.to.you)
table(conjoint_know$Closeness.with.you)
table(conjoint_know$Apparent.knowledge.of.the.person)
table(conjoint_know$Apparent.concern.of.the.person)
table(conjoint_know$Apparent.motivation.of.the.person)


```

#CHANGE VARIABLE ORDER AND SET REFERENCE CATEGORIES

```{r}

#REORDER LEVELS
conjoint_know$Topic <-                             factor(conjoint_know$Topic,
                                                  levels = c("pesticides", "floods", "lifestyle diseases", "infectious diseases"))
conjoint_know$Gender.of.the.person <-              factor(conjoint_know$Gender.of.the.person,
                                                  levels = c(  "male", "female"))
conjoint_know$Relation.to.you <-                   factor(conjoint_know$Relation.to.you,
                                                  levels = c("colleague/fellow student", "friend", "relative"))
conjoint_know$Closeness.with.you <-                factor(conjoint_know$Closeness.with.you,
                                                  levels = c("not close", "somewhat close", "fairly close", "very close"))
conjoint_know$Apparent.knowledge.of.the.person <-  factor(conjoint_know$Apparent.knowledge.of.the.person,
                                                  levels = c("not well-informed", "somewhat well-informed", "fairly well-informed", "very well-informed"))
conjoint_know$Apparent.concern.of.the.person <-    factor(conjoint_know$Apparent.concern.of.the.person,
                                                  levels = c("not concerned", "somewhat concerned", "fairly concerned", "very concerned"))
conjoint_know$Apparent.motivation.of.the.person <- factor(conjoint_know$Apparent.motivation.of.the.person,
                                                  levels = c("to convince you to take more precautions", "to convince you to take fewer precautions", "to feel less anxious", "to exchange information with you"))


```

######RUN MAIN AMCE, MM, AND DIFFERENCE IN MM (AVERAGE KNOWLEDGE)

```{r}
###AMCE

#Full model
amce_know <- cj(conjoint_know, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ know_cat, 
                  id = ~ Response.ID,
                  estimate = "amce")


# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

amce_know_plot <- ggplot(amce_know, aes(x = estimate, y = level, color = know_cat, shape = know_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("violet", "darkorchid4")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes by general knowledge", color = "General knowledge", shape = "General knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.3, 0.3)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/amce_know.png", plot = amce_know_plot, width = 8, height = 6, dpi = 300)


###MARGINAL MEANS

mm_know <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat, 
                  id = ~ Response.ID,
                  estimate = "mm")


mm_know$feature <- factor(mm_know$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

mm_know_plot <- ggplot(mm_know, aes(x = estimate, y = level, color = know_cat, shape = know_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("violet", "darkorchid4")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes by general knowledge", color = "General knowledge", shape = "General knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(0.3, 0.7)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/mm_know.png", plot = mm_know_plot, width = 8, height = 6, dpi = 300)


###DIFFERENCE IN MARGINAL MEANS

diff_know <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat, 
                  id = ~ Response.ID,
                  estimate = "mm_difference")


diff_know$feature <- factor(diff_know$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

diff_know_plot <- ggplot(diff_know, aes(x = estimate, y = level, color = know_cat, shape = know_cat)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkorchid4")) + 
  scale_shape_manual(values = c(17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Difference in Marginal Means", y = "All attributes by general knowledge", color = "General knowledge", shape = "General knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.15, 0.15)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/diff_know.png", plot = diff_know_plot, width = 8, height = 6, dpi = 300)

```

######RUN MAIN AMCE, MM, AND DIFFERENCE IN MM (FLOOD KNOWLEDGE)

```{r}
###AMCE

#Full model
amce_know_flood <- cj(conjoint_know, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ know_cat_flood, 
                  id = ~ Response.ID,
                  estimate = "amce")


# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

amce_know_flood_plot <- ggplot(amce_know_flood, aes(x = estimate, y = level, color = know_cat_flood, shape = know_cat_flood)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("pink", "darkorchid2")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes by flood knowledge", color = "Flood knowledge", shape = "Flood knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.3, 0.3)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/amce_know_flood.png", plot = amce_know_flood_plot, width = 8, height = 6, dpi = 300)


###MARGINAL MEANS

mm_know_flood <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat_flood, 
                  id = ~ Response.ID,
                  estimate = "mm")


mm_know_flood$feature <- factor(mm_know_flood$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

mm_know_flood_plot <- ggplot(mm_know_flood, aes(x = estimate, y = level, color = know_cat_flood, shape = know_cat_flood)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("pink", "darkorchid2")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes by flood knowledge", color = "Flood knowledge", shape = "Flood knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(0.3, 0.7)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/mm_know_flood.png", plot = mm_know_flood_plot, width = 8, height = 6, dpi = 300)


###DIFFERENCE IN MARGINAL MEANS

diff_know_flood <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat_flood, 
                  id = ~ Response.ID,
                  estimate = "mm_difference")


diff_know_flood$feature <- factor(diff_know_flood$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

diff_know_flood_plot <- ggplot(diff_know_flood, aes(x = estimate, y = level, color = know_cat_flood, shape = know_cat_flood)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkorchid2")) + 
  scale_shape_manual(values = c(17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Difference in Marginal Means", y = "All attributes by flood knowledge", color = "Flood knowledge", shape = "Flood knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.15, 0.15)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/diff_know_flood.png", plot = diff_know_flood_plot, width = 8, height = 6, dpi = 300)

```

######RUN MAIN AMCE, MM, DIFFERENCE IN MM (PESTICIDE KNOWLEDGE)

```{r}

###AMCE

#Full model
amce_know_pest <- cj(conjoint_know, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ know_cat_pest, 
                  id = ~ Response.ID,
                  estimate = "amce")


# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

amce_know_pest_plot <- ggplot(amce_know_pest, aes(x = estimate, y = level, color = know_cat_pest, shape = know_cat_pest)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("pink", "darkorchid2")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes by pesticide knowledge", color = "Pesticide knowledge", shape = "Pesticide knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.3, 0.3)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/amce_know_pest.png", plot = amce_know_pest_plot, width = 8, height = 6, dpi = 300)


###MARGINAL MEANS

mm_know_pest <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat_pest, 
                  id = ~ Response.ID,
                  estimate = "mm")


mm_know_pest$feature <- factor(mm_know_pest$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

mm_know_pest_plot <- ggplot(mm_know_pest, aes(x = estimate, y = level, color = know_cat_pest, shape = know_cat_pest)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("pink", "darkorchid2")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes by pesticide knowledge", color = "Pesticide knowledge", shape = "Pesticide knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(0.3, 0.7)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/mm_know_pest.png", plot = mm_know_pest_plot, width = 8, height = 6, dpi = 300)


###DIFFERENCE IN MARGINAL MEANS

diff_know_pest <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat_pest, 
                  id = ~ Response.ID,
                  estimate = "mm_difference")


diff_know_pest$feature <- factor(diff_know_pest$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

diff_know_pest_plot <- ggplot(diff_know_pest, aes(x = estimate, y = level, color = know_cat_pest, shape = know_cat_pest)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkorchid2")) + 
  scale_shape_manual(values = c(17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes by pesticide knowledge", color = "Pesticide knowledge", shape = "Pesticide knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.15, 0.15)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/diff_know_pest.png", plot = diff_know_pest_plot, width = 8, height = 6, dpi = 300)

```

######RUN MAIN AMCE, MM, AND DIFFERENCE IN MM (INFECTIOUS DISEASE KNOWLEDGE)

```{r}

###AMCE

#Full model
amce_know_infect <- cj(conjoint_know, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ know_cat_infect, 
                  id = ~ Response.ID,
                  estimate = "amce")


# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

amce_know_infect_plot <- ggplot(amce_know_infect, aes(x = estimate, y = level, color = know_cat_infect, shape = know_cat_infect)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("pink", "darkorchid2")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes by infectious disease knowledge", color = "Infectious disease knowledge", shape = "Infectious disease knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.3, 0.3)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/amce_know_infect.png", plot = amce_know_infect_plot, width = 8, height = 6, dpi = 300)


###MARGINAL MEANS

mm_know_infect <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat_infect, 
                  id = ~ Response.ID,
                  estimate = "mm")


mm_know_infect$feature <- factor(mm_know_infect$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

mm_know_infect_plot <- ggplot(mm_know_infect, aes(x = estimate, y = level, color = know_cat_infect, shape = know_cat_infect)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("pink", "darkorchid2")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes by infectious disease knowledge", color = "Infectious disease knowledge", shape = "Infectious disease knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(0.3, 0.7)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/mm_know_infect.png", plot = mm_know_infect_plot, width = 8, height = 6, dpi = 300)


###DIFFERENCE IN MARGINAL MEANS

diff_know_infect <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat_infect, 
                  id = ~ Response.ID,
                  estimate = "mm_difference")


diff_know_infect$feature <- factor(diff_know_infect$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

diff_know_infect_plot <- ggplot(diff_know_infect, aes(x = estimate, y = level, color = know_cat_infect, shape = know_cat_infect)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkorchid2")) + 
  scale_shape_manual(values = c(17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes by infectious disease knowledge", color = "Infectious disease knowledge", shape = "Infectious disease knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.15, 0.15)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/diff_know_infect.png", plot = diff_know_infect_plot, width = 8, height = 6, dpi = 300)


```

######RUN MAIN AMCE, MM, AND DIFFERENCE IN MM (LIFESTYLE DISEASE KNOWLEDGE)

```{r}
###AMCE

#Full model
amce_know_life <- cj(conjoint_know, selected ~ 
             Topic +
             Gender.of.the.person +
             Relation.to.you +
             Closeness.with.you + 
             Apparent.knowledge.of.the.person +
             Apparent.concern.of.the.person +
             Apparent.motivation.of.the.person, 
                  by = ~ know_cat_life, 
                  id = ~ Response.ID,
                  estimate = "amce")


# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

amce_know_life_plot <- ggplot(amce_know_life, aes(x = estimate, y = level, color = know_cat_life, shape = know_cat_life)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("pink", "darkorchid2")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "AMCE", y = "All attributes by lifestyle disease knowledge", color = "Lifestyle disease knowledge", shape = "Lifestyle disease knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.3, 0.3)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/amce_know_life.png", plot = amce_know_life_plot, width = 8, height = 6, dpi = 300)


###MARGINAL MEANS

mm_know_life <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat_life, 
                  id = ~ Response.ID,
                  estimate = "mm")


mm_know_life$feature <- factor(mm_know_life$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

mm_know_life_plot <- ggplot(mm_know_life, aes(x = estimate, y = level, color = know_cat_life, shape = know_cat_life)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("pink", "darkorchid2")) + 
  scale_shape_manual(values = c(16, 17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Marginal Means", y = "All attributes by lifestyle disease knowledge", color = "Lifestyle disease knowledge", shape = "Lifestyle disease knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(0.3, 0.7)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/mm_know_life.png", plot = mm_know_life_plot, width = 8, height = 6, dpi = 300)


###DIFFERENT IN MARGINAL MEANS

diff_know_life <- cj(conjoint_know, selected ~ 
                  Topic +
                  Gender.of.the.person +
                  Relation.to.you +
                  Closeness.with.you + 
                  Apparent.knowledge.of.the.person +
                  Apparent.concern.of.the.person +
                  Apparent.motivation.of.the.person, 
                  by = ~ know_cat_life, 
                  id = ~ Response.ID,
                  estimate = "mm_difference")


diff_know_life$feature <- factor(diff_know_life$feature,
                              levels = c("Topic",
                                         "Relation.to.you",
                                         "Apparent.knowledge.of.the.person",
                                         "Apparent.concern.of.the.person",
                                         "Apparent.motivation.of.the.person",
                                         "Closeness.with.you",
                                         "Gender.of.the.person"))

# Define the dodge width to create separation between low and high risk knowledge lines
dodge_width <- 1

diff_know_life_plot <- ggplot(diff_know_life, aes(x = estimate, y = level, color = know_cat_life, shape = know_cat_life)) +
  geom_point(size = 2, position = position_dodge(width = dodge_width)) +  # Offset points
  geom_errorbarh(aes(xmin = lower, xmax = upper), height = 1, 
                 position = position_dodge(width = dodge_width)) +  # Offset error bars
  geom_vline(xintercept = 0, linetype = "dashed", color = "grey") +  # Add a dashed vertical line at x = 0
  scale_color_manual(values = c("darkorchid2")) + 
  scale_shape_manual(values = c(17)) +  # Custom shapes: 16 = filled circle, 17 = filled triangle
  theme_minimal() +  # Apply a minimal theme
  labs(x = "Difference in Marginal Means", y = "All attributes by lifestyle disease knowledge", color = "Lifestyle disease knowledge", shape = "Lifestyle disease knowledge") + # Customize labels
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # Set the background of the plot panel to white
    plot.background = element_rect(fill = "white", color = NA)  # Set the overall background of the plot to white
  ) +
  xlim(-0.15, 0.15)  # Set the x-axis limits (replace with your desired values)

ggsave("figures/knowledge/diff_know_life.png", plot = diff_know_life_plot, width = 8, height = 6, dpi = 300)

```

#BIVARIATE CORRELATIONS OF TOPIC-SPECIFIC KNOWLEDGE

```{r}

table(conjoint_know$know_flooding)

# Create a dataframe with the five variables
risk_vars <- conjoint_know[, c("knowledge", "rknow_flooding", "rknow_infectious", "rknow_pesticides", "rknow_lifestyle")]

# Calculate the correlation matrix
correlation_matrix <- cor(risk_vars, use = "complete.obs", method = "pearson")

# Display the correlation matrix
print(correlation_matrix)

```

#####DIAGNOSTICS

```{r}

# BALANCE TESTING: GENERAL

# Manually map know_cat to numeric
conjoint_know$know_cat_num <- ifelse(conjoint_know$know_cat == "Less well-informed", 0, 
                                      ifelse(conjoint_know$know_cat == "More well-informed", 1, NA))

# Check for any NA values introduced by this process
table(conjoint_know$know_cat_num)

# Calculate marginal means using your dataset and attributes
knowbalance <- plot(mm(conjoint_know, know_cat_num ~ 
                 Topic +
                 Gender.of.the.person +
                 Apparent.motivation.of.the.person +
                 Apparent.concern.of.the.person +
                 Relation.to.you +
                 Closeness.with.you + 
                 Apparent.knowledge.of.the.person,
                 id = ~Response.ID), 
                xlim = c(0, 0.4), vline = mean(conjoint_know$know_cat_num, na.rm = TRUE))

# Adjust the plot for larger text and dots
knowbalance_adj <- knowbalance +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Balance Testing: General Risk Knowledge") +  # Add the title here
    labs(color = "Attribute") +  # Change the label for the color legend
  theme(
    text = element_text(size = 20),            # Increase base text size
    axis.title = element_text(size = 22),      # Increase axis title size
    axis.text = element_text(size = 18),       # Increase axis tick labels size
    legend.title = element_text(size = 22),    # Increase legend title size
    legend.text = element_text(size = 18),     # Increase legend text size
    plot.title = element_text(size = 24, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18)       # Increase facet labels text size (if using facets)
  ) +
  guides(color = guide_legend(nrow = 4))  # Adjust the number of rows in the legend

# Save the plot
ggsave("diagnostics/balance_know.png", plot = knowbalance_adj, width = 15, height = 11, dpi = 300)


# BALANCE TESTING: FLOOD

# Manually map know_cat_flood to numeric
conjoint_know$know_cat_flood_num <- ifelse(conjoint_know$know_cat_flood == "Low flood knowledge", 0, 
                                      ifelse(conjoint_know$know_cat_flood == "High flood knowledge", 1, NA))

# Check for any NA values introduced by this process
table(conjoint_know$know_cat_flood_num)

# Calculate marginal means using your dataset and attributes
knowbalance_flood <- plot(mm(conjoint_know, know_cat_flood_num ~ 
                 Topic +
                 Gender.of.the.person +
                 Apparent.motivation.of.the.person +
                 Apparent.concern.of.the.person +
                 Relation.to.you +
                 Closeness.with.you + 
                 Apparent.knowledge.of.the.person,
                 id = ~Response.ID), 
                xlim = c(0, 0.7), vline = mean(conjoint_know$know_cat_flood_num, na.rm = TRUE))

# Adjust the plot for larger text and dots
knowbalance_flood_adj <- knowbalance_flood +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Balance Testing: Flood Risk Knowledge") +  # Add the title here
    labs(color = "Attribute") +  # Change the label for the color legend
  theme(
    text = element_text(size = 20),            # Increase base text size
    axis.title = element_text(size = 22),      # Increase axis title size
    axis.text = element_text(size = 18),       # Increase axis tick labels size
    legend.title = element_text(size = 22),    # Increase legend title size
    legend.text = element_text(size = 18),     # Increase legend text size
    plot.title = element_text(size = 24, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18)       # Increase facet labels text size (if using facets)
  ) +
  guides(color = guide_legend(nrow = 4))  # Adjust the number of rows in the legend

# Save the plot
ggsave("diagnostics/balance_know_flood.png", plot = knowbalance_flood_adj, width = 15, height = 11, dpi = 300)


# BALANCE TESTING: PESTICIDES

# Manually map know_cat_pest to numeric (assuming "Low pesticide RP" and "High pesticide knowledge")
conjoint_know$know_cat_pest_num <- ifelse(conjoint_know$know_cat_pest == "Low pesticide knowledge", 0, 
                                      ifelse(conjoint_know$know_cat_pest == "High pesticide knowledge", 1, NA))

# Check for any NA values introduced by this process
table(conjoint_know$know_cat_pest_num)

# Calculate marginal means using your dataset and attributes
knowbalance_pest <- plot(mm(conjoint_know, know_cat_pest_num ~ 
                 Topic +
                 Gender.of.the.person +
                 Apparent.motivation.of.the.person +
                 Apparent.concern.of.the.person +
                 Relation.to.you +
                 Closeness.with.you + 
                 Apparent.knowledge.of.the.person,
                 id = ~Response.ID), 
                xlim = c(0, 0.5), vline = mean(conjoint_know$know_cat_pest_num, na.rm = TRUE))

# Adjust the plot for larger text and dots
knowbalance_pest_adj <- knowbalance_pest +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Balance Testing: Pesticide Risk Knowledge") +  # Add the title here
    labs(color = "Attribute") +  # Change the label for the color legend
  theme(
    text = element_text(size = 20),            # Increase base text size
    axis.title = element_text(size = 22),      # Increase axis title size
    axis.text = element_text(size = 18),       # Increase axis tick labels size
    legend.title = element_text(size = 22),    # Increase legend title size
    legend.text = element_text(size = 18),     # Increase legend text size
    plot.title = element_text(size = 24, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18)       # Increase facet labels text size (if using facets)
  ) +
  guides(color = guide_legend(nrow = 4))  # Adjust the number of rows in the legend

# Save the plot
ggsave("diagnostics/balance_know_pest.png", plot = knowbalance_pest_adj, width = 15, height = 11, dpi = 300)


# BALANCE TESTING: INFECTIOUS DISEASES

# Manually map know_cat_infect to numeric (assuming "Low infectious disease RP" and "High infectious disease knowledge")
conjoint_know$know_cat_infect_num <- ifelse(conjoint_know$know_cat_infect == "Low infectious disease knowledge", 0, 
                                      ifelse(conjoint_know$know_cat_infect == "High infectious disease knowledge", 1, NA))

# Check for any NA values introduced by this process
table(conjoint_know$know_cat_infect_num)

# Calculate marginal means using your dataset and attributes
knowbalance_infect <- plot(mm(conjoint_know, know_cat_infect_num ~ 
                 Topic +
                 Gender.of.the.person +
                 Apparent.motivation.of.the.person +
                 Apparent.concern.of.the.person +
                 Relation.to.you +
                 Closeness.with.you + 
                 Apparent.knowledge.of.the.person,
                 id = ~Response.ID), 
                xlim = c(0, 0.7), vline = mean(conjoint_know$know_cat_infect_num, na.rm = TRUE))

# Adjust the plot for larger text and dots
knowbalance_infect_adj <- knowbalance_infect +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Balance Testing: Infectious Disease Risk Knowledge") +  # Add the title here
    labs(color = "Attribute") +  # Change the label for the color legend
  theme(
    text = element_text(size = 20),            # Increase base text size
    axis.title = element_text(size = 22),      # Increase axis title size
    axis.text = element_text(size = 18),       # Increase axis tick labels size
    legend.title = element_text(size = 22),    # Increase legend title size
    legend.text = element_text(size = 18),     # Increase legend text size
    plot.title = element_text(size = 24, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18)       # Increase facet labels text size (if using facets)
  ) +
  guides(color = guide_legend(nrow = 4))  # Adjust the number of rows in the legend

# Save the plot
ggsave("diagnostics/balance_know_infect.png", plot = knowbalance_infect_adj, width = 15, height = 11, dpi = 300)


# BALANCE TESTING: LIFESTYLE DISEASES

# Manually map know_cat_life to numeric
conjoint_know$know_cat_life_num <- ifelse(conjoint_know$know_cat_life == "Low lifestyle disease knowledge", 0, 
                                      ifelse(conjoint_know$know_cat_life == "High lifestyle disease knowledge", 1, NA))

# Check for any NA values introduced by this process
table(conjoint_know$know_cat_life_num)

# Calculate marginal means using your dataset and attributes
knowbalance_life <- plot(mm(conjoint_know, know_cat_life_num ~ 
                 Topic +
                 Gender.of.the.person +
                 Apparent.motivation.of.the.person +
                 Apparent.concern.of.the.person +
                 Relation.to.you +
                 Closeness.with.you + 
                 Apparent.knowledge.of.the.person,
                 id = ~Response.ID), 
                xlim = c(0, 1.4), vline = mean(conjoint_know$know_cat_life_num, na.rm = TRUE))

# Adjust the plot for larger text and dots
knowbalance_life_adj <- knowbalance_life +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
    ggtitle("Balance Testing: Lifestyle Disease Risk Knowledge") +  # Add the title here
    labs(color = "Attribute") +  # Change the label for the color legend
  theme(
    text = element_text(size = 20),            # Increase base text size
    axis.title = element_text(size = 22),      # Increase axis title size
    axis.text = element_text(size = 18),       # Increase axis tick labels size
    legend.title = element_text(size = 22),    # Increase legend title size
    legend.text = element_text(size = 18),     # Increase legend text size
    plot.title = element_text(size = 24, face = "bold"),  # Increase plot title size and style
    strip.text = element_text(size = 18)       # Increase facet labels text size (if using facets)
  ) +
  guides(color = guide_legend(nrow = 4))  # Adjust the number of rows in the legend

# Save the plot
ggsave("diagnostics/balance_know_life.png", plot = knowbalance_life_adj, width = 15, height = 11, dpi = 300)

```


